package com.naihe.anderson.sms.manager;

import java.util.ArrayList;
import java.util.List;

import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Handler;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.dao.SystemDao;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.util.FileUtil;
import com.naihe.anderson.sms.util.ObjectUtil;

public class ContactMgr {

	NhSmsApp application;
	SystemDao sysContactDao;
	PersonObserver personObserver;
	
	private List<ContactUser> contactList;
	private boolean isGetSysContactData = false;
	
	//处理通讯录好友发生变更时的动作
	private class PersonObserver extends ContentObserver {
		public PersonObserver(Handler handler) {
			super(handler);
		}

		public void onChange(boolean selfChange) {
			FileUtil.addLog("ContactMgr::PersonObserver onChange", ContactMgr.class, "ContactMgr", null);
			changeContactData();
		}
	}
	
	public ContactMgr(NhSmsApp app){
		this.application = app;
		sysContactDao = new SystemDao(app);
		contactList = new ArrayList<ContactUser>();
	}
	
	public void init(){
		initSysContactsChangeListener();
		firstGetContactData();
	}
	
	private void initSysContactsChangeListener() {
		//注册通讯录好友变更接收器
	    personObserver = new PersonObserver(new Handler());
	    application.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, personObserver);		
	}
	
	/**
	 * 第一次获取系统通讯录数据
	 */
	public void firstGetContactData() {
		new FirstGetContactDataTask().execute();
	}
	
	public void changeContactData() {
		new ChangeContactDataTask().execute();
	}
	
	/**
	 * 获取联系人头像
	 * @param contactId
	 * @return
	 */
	public Bitmap getContactHeadImgByPhotoId(Long photoId){
		return sysContactDao.getContactHeadImgByPhontoId(photoId);
	}
	
	/**
	 * 根据ID获取联系人
	 */
	public ContactUser getContactUserById(Long contactId) {
		if (contactId == null || contactId <= 0) {
			return null;
		}

		for (ContactUser contactUser : contactList) {
			if(contactId == contactUser.getContactId()){
				return contactUser;
			}
		}
		return null;
	}
	
	/**
	 * 根据ID获取联系人
	 */
	public Long getContactIdByPhoneNum(String phoneNum) {
		if (TextUtils.isEmpty(phoneNum)) {
			return null;
		}
		
		phoneNum = ObjectUtil.phoneNumberFormatPrefix(phoneNum);
		
		return sysContactDao.getContactIdByPhoneNum(phoneNum);
	}
	
	/**
	 * 获取通讯录，一个号码一个联系人
	 * 
	 * @return
	 */
	public List<ContactUser> getContactData1T1() {
		List<ContactUser> tempContactUserList = new ArrayList<ContactUser>();
		ContactUser cUser = null;
		for (ContactUser contactUser : contactList) {
			for (String num : contactUser.getNumberList()) {
				cUser = new ContactUser();
				cUser.setName(contactUser.getName());
				cUser.setChineseNameCode(contactUser.getChineseNameCode());
				cUser.setNumbers(num);
				cUser.setPhotoId(contactUser.getPhotoId());
				cUser.setContactId(contactUser.getContactId());
				tempContactUserList.add(cUser);
			}
		}
		return tempContactUserList;
	}
	//=================================================================================================================================
	
	//第一次读取通讯录数据任务
	class FirstGetContactDataTask extends AsyncTask<Object, Object, Object>{
		List<ContactUser> tempContactUserList;
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected Object doInBackground(Object... arg0) {
			tempContactUserList = sysContactDao.getContactUserListFromSys();
			return true;
		}
		
        protected void onPostExecute(Object result) {
        	contactList.clear();
	        contactList.addAll(tempContactUserList);
	        isGetSysContactData = true;
	        FileUtil.addLog("ContactMgr::FirstGetContactDataTask finis", ContactMgr.class, "ContactMgr", null);
	        application.getNhSmsMgr().initData();
	        application.getSignatureMsgMgr().init();
	        application.getNoteMgr().init();
        }		
	}
	
	class ChangeContactDataTask extends AsyncTask<Object, Object, Object>{
		List<ContactUser> tempContactUserList;
		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected Object doInBackground(Object... arg0) {
			tempContactUserList = sysContactDao.getContactUserListFromSys();
			return true;
		}
		
        protected void onPostExecute(Object result) {
        	contactList.clear();
	        contactList.addAll(tempContactUserList);
	        isGetSysContactData = true;
	        FileUtil.addLog("ContactMgr::ChangeContactDataTask finis", ContactMgr.class, "ContactMgr", null);
	        application.getNhSmsMgr().refreshNhsmsData();
        }		
	}

	public boolean isGetSysContactData() {
		return isGetSysContactData;
	}	
}
